	$(function() {
	    reload(ajaxGetItem);
	    //请求列表数据
	    // ajaxGetItem();

	    //加载标题
		NativeObj.updateTitle('预约挂号');
	});
	//订单对象
	var indent = {
		access_token: '',
		category : '',
		code : '',
		date : '',
		time : '',
		doctorname: ''
	}

	//科室详情切换
	$('.detail-btn').on('click', function(){
		if($('.ks-intro').hasClass('intro-detail')){
			$('.detail-btn').text('详情');
			$('.ks-intro').removeClass('intro-detail');
		}
		else {
			$('.detail-btn').text('收起');
			$('.ks-intro').addClass('intro-detail');
		}
	});

	//可预约医师点击下拉
	$('.doctor-group').on('click', '.doctor-item:not(.doctor-off) .doctor-wrap', function(){
		$('.am-collapse').collapse({
		  toggle: false
		}).on('open.collapse.amui', function() {
			$(this).parents('.doctor-item').find('.down-icon img').attr('src', '../images/up.png');
		}).on('close.collapse.amui', function() {
			$(this).parents('.doctor-item').find('.down-icon img').attr('src', '../images/down.png');
		});
		$('.am-collapse').collapse('close');
		$(this).next().find('.am-collapse').collapse('open');
	});

	//预约选择就诊时间
	$('.doctor-group').on('click', '.order-count', function(){
		var $nav = $(this).parents('nav');
		indent.code = $nav.data("code");
		indent.doctorname = $nav.data('doctorname');
		var date = $(this).data('scheduletime').split(';');
		indent.date = date.splice(0,1)[0];
		$('.doctor-YMD span').text(indent.date);
		var HMS = $('.doctor-HMS');
		HMS.empty();
		$.each(date, function(i, v){
			var $time_item = '<label for="clinic_time_' + i + '" class="am-radio"><input id="clinic_time_' + i + '" type="radio" name="clinic_time" value="' + v + '">' + v + '</label>';
			HMS.append($time_item);
		});

	    $modal = $('#select-modal');
	    $modal.modal({
	    	closeViaDimmer: true,
	    	closeOnConfirm: false,
	    	onConfirm: getValue
	    });
	});

	//得到预约时间
	function getValue(){
		var $modal = $('#select-modal');

		var val = $modal.find('input[name="clinic_time"]:checked').val();
		if(val === undefined){
			$modal = $('#alert-modal');
			$modal.find('.am-modal-bd').text('请选择就诊时间!');
		    $modal.modal({
		    	closeViaDimmer: true,
		    	closeOnConfirm: false,
		    	onConfirm: getValue
		    });
		}
		else {
			indent.time = val;
			window.location = 'appointment-register.html?access_token=' + indent.access_token + '&speciality_name=' + indent.category + '&doctor_code=' + indent.code + '&date=' + indent.date + '&time=' + indent.time + '&doctor_name=' + indent.doctorname;
			$modal.modal("close");
		}
	}

	//请求列表数据
    function ajaxGetItem(){
    	loadingModal.start();

    	var obj = getGetData();
    	indent.access_token = obj.access_token;
    	var code = obj.code;
    	var scode = obj.scode ? obj.scode : '';

    	var data = {
    		'access_token' : indent.access_token
    	}
    	var url = '';
       	if(scode){
    		data['doctor_code'] = code;
    		data['speciality_code'] = scode;
    		url = '/hospital/get-schedule-by-doctor';
    	}
    	else {
    		data['speciality_code'] = code;
    		obj.date && (data['date'] = obj.date);
    		url = '/hospital/get-schedule-info';
    	}

    	$.ajax({
    		url:  host + url,
    		type: 'GET',
    		data: data,
    		success: function(data){
    			$('#category').text(data.name);
    			indent.category = data.name;
    			var $p = $('.ks-intro p');
				data.info = data.info.replace(/\n/gi,"<br />");
    			$p.html(data.info).parents('.am-intro-bd');

    			$.each(data.items, function(i, v){
    				setItem(v);
    			})
    		},
    		error: function(err){
				errorModal(err.responseJSON);
    		},
    		complete: function(){
				loadingModal.stop();
				if($('.ks-intro p').height() < 70){
    				$('.detail-btn').remove();
    			}
    		}
    	})
    }
    //加载列表
    function setItem(obj){
    	obj.DoctorCode = obj.DoctorCode ? obj.DoctorCode : ''
    	$ul = $('.doctor-group>ul');
    	var $li = $('<li class="am-g doctor-item"></li>');
    	var $wrap =  $('<div class="doctor-wrap"></div>');
    	var $picture = $(
    		'<div class="am-u-sm-2 doctor-picture">'+
				'<img src="' + host + '/user/get-registration-doctor-avatar?doctor_code=' + obj.DoctorCode + '" alt="picture">'+ //头像图片路径
			'</div>'
		);
    	var $data = $(
			'<div class="am-u-sm-4 doctor-data">'+
				'<span class="doctor-name">' + obj.DoctorName + '</span>'+ //名字
				'<span class="doctor-post">' + obj.ClinicName + '</span>'+ //主治医师出诊
			'</div>'
		);
    	var $time = $(
			'<div class="am-u-sm-4 doctor-time am-vertical-align">'+
				'<span class="am-vertical-align-middle">' + obj.ClinicTime + '出诊</span>'+ //出诊时间 例：周二、三上午出诊
			'</div>'
		);
    	var $state = $(
			'<div class="am-u-sm-2 doctor-state">'+
				'<div class="doctor-on">'+
					'<a href="javascript:;">' + obj.Status + '</a>'+ //可预约 / 已满
					'<i class="down-icon">'+
						'<img src="../images/down.png" alt="down-icon">'+
					'</i>'+
				'</div>'+
			'</div>'
		);
		$wrap.append($picture);
		$wrap.append($data);
		$wrap.append($time);
		$wrap.append($state);
		$li.append($wrap);
		if(obj.Status === '已满'){
			$li.addClass('doctor-off');
		}
		else {
			var $nav = $(
				'<nav class="am-u-sm-12" data-code=' + obj.DoctorCode + ' data-doctorname=' + obj.DoctorName + '>'+
					'<ul class="am-nav am-collapse">'+
				    '<li>'+
					    '<table class="doctor-time-table">'+
						  	'<tr>'+
						  		'<th>日期</th>'+
						  		'<th>上午</th>'+
						  		'<th>下午</th>'+
						  	'</tr>'+
					  	'</table>'+
				    '</li>'+
				  '</ul>'+
				'</nav>'
			);
			for(var i=0; i<obj.ScheduleList.length; i++){
				var schedule = obj.ScheduleList[i];
				var $tr = $('<tr></tr>');
				var $date = $('<td>' + schedule.ScheduleDate + '<br>' + schedule.Week + '</td>');
				var $morning = schedule.AMCount ? $('<td><span class="order-count" data-scheduletime=' + schedule.ScheduleDate + ';' + schedule.AMScheduleTime + '>剩下' + schedule.AMCount + '人</span></td>') : $('<td></td>');
				var $afternoon = schedule.PMCount ? $('<td><span class="order-count" data-scheduletime=' + schedule.ScheduleDate + ';' + schedule.PMScheduleTime + '>剩下' + schedule.PMCount + '人</span></td>') : $('<td></td>');
				$tr.append($date);
				$tr.append($morning);
				$tr.append($afternoon);
				$nav.find('.doctor-time-table').append($tr);
			}
			$li.append($nav);
		}
		$ul.append($li);
    }
